from pymongo import MongoClient
from selenium import webdriver
from bs4 import BeautifulSoup
from pprint import pprint
import requests
import datetime
import json
import time
import re

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

driver = webdriver.Chrome(executable_path = '/Users/xuchaosheng/Workspace/KNX/libs/chromedriver')


def is_element_visible(element):
    try:
        the_element = EC.visibility_of_element_located(element)
        assert the_element(driver)
        flag = True
    except:
        flag = False

    return flag


client = MongoClient('127.0.0.1', 27017)
db = client['knx_posts_db']
offical_posts_coll = db['offical_posts_coll']


class POST():
    def __init__(self):
        self.scrapy()

    def scrapy(self):
        driver.get("https://www.nio.com/careers")

        time.sleep(3)

        while is_element_visible((By.ID, "show-more")):
            driver.find_element_by_id('show-more').click()

        time.sleep(2)

        urls = [ele.get_attribute('data-href') for ele in driver.find_elements_by_class_name('clickable-row')]

        for url in urls:
            driver.get(url)
            time.sleep(1)
            try:
                name = driver.find_element_by_class_name('contentlinepanel').find_elements_by_tag_name('span')[2].text
                description = driver.find_element_by_id('requisitionDescriptionInterface.ID1323.row1').text
                location = driver.find_element_by_id('requisitionDescriptionInterface.ID1432.row1').text
                date = driver.find_element_by_id('requisitionDescriptionInterface.reqPostingDate.row1').text

                item = {
                    "url": url,
                    'edu': '',
                    'exp': [],
                    'name': name,
                    'date': date,
                    'lang': '',
                    'place': '',
                    'major': '',
                    'count': '',
                    'salary': [],
                    'toSchool': True,
                    'welfare': [],
                    'funType': '',
                    'company': '蔚来汽车',
                    'location': location,
                    'industry': '汽车制造',
                    'keywords': [],
                    'platform': 'offical',
                    'searchKeyword': '',
                    'description': description[:description.find('了解更多招聘信息')].strip(),
                    'subIndustry': '',
                    'stime': datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
                }

                print(item['company'], item['name'])
                if not offical_posts_coll.find_one({'name': item['name'], 'company': item['company'], 'location': item['location']}):
                    offical_posts_coll.insert_one(item)
            except:
                continue


POST()
